<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/5/3
 * Time: 14:06
 */
namespace app\first\model;

use app\admin\model\Pic;
use think\Model;

class Auth extends Model{
    protected $autoWriteTimestamp = true;

    /**
     * @param int $mid
     * @param string $id_cards
     * @param int $type
     * @param string $licence
     * @param string $real_name
     * @param int $sex
     * @param string $company_name
     * @param string $company_type
     * @param string $company_business
     * @param null $out
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function addNew($mid=0,$id_cards='',$type=1,$licence='',$real_name='',$sex=1,$company_name='',$company_type='',$company_business='',&$out=null){
        $_type = $type==1 ? '个人' : '企业';
        $title = "{$real_name}{$_type}认证";
        $data = [
            'title'=>$title,
            'mid'=>$mid,
            'id_cards'=>$id_cards,
            'type'=>$type,
            'licence'=>$licence,
            'real_name'=>$real_name,
            'sex'=>$sex,
            'company_name'=>$company_name,
            'company_type'=>$company_type,
            'company_business'=>$company_business,
        ];
        $valid = new \app\first\validate\Auth();
        if(!$valid->check($data)){
            $out = $valid->getError();
            return false;
        }
        if($_type==2){
            if(!$company_name){
                $out = '请填写公司名';
                return false;
            }
            if(!$licence){
                $out = '请填写营业执照';
                return false;
            }
            if(!$company_type){
                $out = '请填写公司类型';
                return false;
            }
            if(!$company_business){
                $out = '请填写营业范围';
                return false;
            }
        }
        $info = $this->where('mid','eq',$mid)->find();
        if($info){
            $data['status'] = 1;
            if(!$info->save($data)){
                $out = '申请失败';
                return false;
            }else{
                return true;
            }
        }else{
            if(!$this->save($data)){
                $out = '申请失败';
                return false;
            }else{
                return true;
            }
        }
    }

    /**
     * @param int $id
     * @param string $field
     * @return array|\PDOStatement|string|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getOneById($id = 0, $field = '*'){
        $info = $this
            ->field($field)
            ->with('member')
            ->where('mid','eq',$id)
            ->find();
        return $info;
    }

    public function member(){
        return $this->hasOne('member','id','mid')
            ->bind([
                'account',
                'nickname',
                'avatar',
                'is_auth',
                'auth_time'
            ]);
    }

    /**
     * @param $value
     * @return array|bool|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getIdCardsAttr($value){
        if($value){
            $pic = new Pic();
            $url = $pic->getUrlsById(explode(',',$value));
            return $url;
        }else{
            return $value;
        }
    }

    /**
     * @param $value
     * @return array|bool|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getLicenceAttr($value){
        if($value){
            $pic = new Pic();
            $url = $pic->getUrlsById(explode(',',$value));
            return $url;
        }else{
            return $value;
        }
    }
}